const getDateObj = function getDateObj(date = new Date()) {
	if (typeof date !== 'object') {
		date = new Date(date.replace(/-/g, '/'))
	}
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0');
	const day = String(date.getDate()).padStart(2, '0');
	const full = year + '-' + month + '-' + day
	const week = date.getDay();
	return {
		year,
		month,
		day,
		week,
		full
	}
}

const getMonthDays = function(date = new Date()) {
	const dateObj = getDateObj(date)
	const year = Number(dateObj.year)
	const month = Number(dateObj.month)
	const full = year + '-' + dateObj.month + '-' + dateObj.day
	const day_num = new Date(year, month, 0).getDate()
	let arr = []
	for (let i = 1; i <= day_num; i++) {
		arr.push({
			day: i,
			month,
			year,
			full: year + '-' + String(month).padStart(2, '0') + '-' + String(i).padStart(2, '0'),
			disable: true,
			disabled: false
		})
	}
	return arr
}

const getPreMonthDays = function(date = new Date()) { // 获取上月在本月日历卡片中剩余天数
	const dateObj = getDateObj(date)
	const year = Number(dateObj.year)
	const month = Number(dateObj.month)
	const full = year + '-' + dateObj.month + '-' + dateObj.day
	const days_num = new Date(year, month - 1, 1).getDay(); // 这个月有几天在下月日历卡片中展示
	let arr = []
	const day_num = new Date(year, month - 1, 0).getDate(); // 这个月共多少天
	for (let i = day_num; i > day_num - days_num; i--) {
		arr.push({
			day: i,
			month: month - 1,
			year,
			full: year + '-' + String(month - 1).padStart(2, '0') + '-' + String(i).padStart(2, '0'),
			disable: true,
			disabled: true
		})
	}
	arr.reverse()
	return arr
}

const getNextMonthDays = function(date = new Date()) {
	const dateObj = getDateObj(date)
	const curMonthDays = getMonthDays(date)
	const preMonthDays = getPreMonthDays(date)
	const year = Number(dateObj.year)
	const month = Number(dateObj.month)
	let len = 42 - (curMonthDays.length + preMonthDays.length)
	let arr = []
	for (let i = 1; i <= len; i++) {
		arr.push({
			day: i,
			month: month + 1,
			year,
			full: year + '-' + String(month + 1).padStart(2, '0') + '-' + String(i).padStart(2, '0'),
			disable: true,
			disabled: true
		})
	}
	return arr
}

const getBeforeDate = function(strDate, n) { //strDate 为字符串日期 如:'2019-01-01' n为你要传入的参数，当前为0，前一天为-1，后一天为1
	const datt = strDate.split('-'); //这边给定一个特定时间
	let newDate = new Date(datt[0], datt[1] - 1, datt[2]);
	// const befminuts = newDate.getTime() + 1000 * 60 * 60 * 24 * parseInt(n); //计算前几天用减，计算后几天用加，最后一个就是多少天的数量 （单位天）
	const befminuts = newDate.getTime() + parseInt(n) * 60 * 1000 * 60; //计算前几天用减，计算后几天用加，最后一个就是多少天的数量(单位小时)
	let beforeDat = new Date();
	beforeDat.setTime(befminuts);
	const befMonth = beforeDat.getMonth() + 1;
	const mon = befMonth >= 10 ? befMonth : '0' + befMonth;
	const befDate = beforeDat.getDate();
	const da = befDate >= 10 ? befDate : '0' + befDate;
	newDate = beforeDat.getFullYear() + '-' + mon + '-' + da;
	return newDate;
}

// 拼车包车租车用当前时间获取 间隔时间之后的时间
const pinDateFn = function(n) { //strDate 为字符串日期 如:'2019-01-01' n为你要传入的参数，当前为0，前一天为-1，后一天为1
	let initDate = new Date()
	// const befminuts = newDate.getTime() + 1000 * 60 * 60 * 24 * parseInt(n); //计算前几天用减，计算后几天用加，最后一个就是多少天的数量 （单位天）
	const befminuts = initDate.getTime() + parseInt(n) * 60 * 1000 * 60; //计算前几天用减，计算后几天用加，最后一个就是多少天的数量(单位小时)
	let beforeDat = new Date();
	beforeDat.setTime(befminuts);
	const befMonth = beforeDat.getMonth() + 1;
	const mon = befMonth >= 10 ? befMonth : '0' + befMonth;
	const befDate = beforeDat.getDate();
	const da = befDate >= 10 ? befDate : '0' + befDate;
	let newDate = beforeDat.getFullYear() + '-' + mon + '-' + da;
	return newDate;
}

//日期转时间戳
const transdate = function transdate(time) {
	console.log('time',time);
	var date = new Date();
	date.setFullYear(time.substring(0, 4));
	date.setMonth(time.substring(5, 7) - 1,time.substring(8, 10));
	date.setDate(time.substring(8, 10));
	if(time.length > 10){
		date.setHours(time.substring(11, 13));
		date.setMinutes(time.substring(14, 16));
		date.setSeconds(time.substring(17, 19));
	}
	// console.log('date',date);
	return Date.parse(date) / 1000;
}
// console.log(transdate("2018-05-28 23:24:12"));  // 1590679452
// console.log(transdate("2018-05-28"));  // 1527436800   (2018-05-28 00:00:00)


const getWeek = function(date = new Date(), addDay = 0) {
	const dateObj = getDateObj(date)
	const year = Number(dateObj.year)
	const month = Number(dateObj.month)
	const day = Number(dateObj.day)
	const curMonthDays = getMonthDays(date)
	const preMonthDays = getPreMonthDays(date)
	const monthDays = preMonthDays.concat(curMonthDays).concat(getNextMonthDays(date))
	let arr = []
	for (let i = 0; i < monthDays.length; i++) {
		if (i % 7 === 0) {
			arr[parseInt(i / 7)] = new Array(7)
		}
		arr[parseInt(i / 7)][i % 7] = monthDays[i]
	}
	return arr
}

// 生成日历
const getCalendar = function(date = new Date(), addDay = 0, endDateDie) {
	let arr = []
	const dateObj = getDateObj(date)
	const year = Number(dateObj.year)
	const month = Number(dateObj.month)
	let  day = Number(dateObj.day)
	const days = getMonthDays(date).length
	const preMonthDays = getPreMonthDays(date)
	const nextMonthDays = getNextMonthDays(date)
	const nextMonthTotDays = getMonthDays(new Date(year, month, day))
	const curMonthDays = getMonthDays(date)  // 这个月的日期数组 例 03-01 --- 03-31
	// let totLength = preMonthDays.length + curMonthDays.length + addDay
	let totLength = preMonthDays.length + curMonthDays.length + (addDay / 24 / 60)
	let totMonthDays = preMonthDays.concat(curMonthDays).concat(nextMonthDays)  // 上个月剩余天数 + 这个月 + 下个月在本页
	let len = 1 // 需要展示几张日历卡片
	const startDate = dateObj.full // 开始时间
	let endDate = getBeforeDate(startDate, addDay) // 截止时间
	if (endDateDie && new Date(endDateDie) < new Date(endDate)) {
		endDate = endDateDie
	}
	totMonthDays.forEach((it_t, in_t) => {
		if (it_t.year === year && it_t.month === month && it_t.day === day) {
			let nextMonth = month + 1
			let nextYear = year
			let surplusDays = (addDay / 24 / 60) - (days - day + 1)
			let nextMonthDays = new Date(nextYear, nextMonth, 0).getDate()
			if (surplusDays > days - day + 1) {
				len += 1
			}
			while (surplusDays - nextMonthDays > 0) {
				surplusDays = surplusDays - nextMonthDays
				nextMonth += 1
				if (nextMonth > 12) {
					nextYear += 1
					nextMonth = nextMonth % 12
				}
				nextMonthDays = new Date(nextYear, nextMonth, 0).getDate()
				len += 1
			}
		}
	})

	for (let i = 0; i < len; i++) {
		let curMonth = month + i
		let curYear = year
		if (curMonth > 12) {
			curYear += 1
			curMonth = curMonth % 12
		}
		if(curMonth === 2){
			day = 28
		}
		day = new Date(curYear, curMonth, 0).getDate()
		const weeks = getWeek(new Date(curYear, curMonth - 1, day), addDay)
		arr.push({
			date: {
				year: curYear,
				month: curMonth
			},
			weeks: weeks
		})
	}
	arr.forEach((it_c, in_c) => {
		it_c.weeks.forEach((it_w, in_w) => {
			it_w.forEach((it_d, in_d) => {
				if (new Date(it_d.full) < new Date(endDate) && new Date(it_d.full) >= new Date(startDate) && !it_d.disabled) {
					return it_d.disable = false
				}
			})
		})
	})
	return arr
}


export default {
	getDateObj,
	getMonthDays,
	getPreMonthDays,
	getNextMonthDays,
	getBeforeDate,
	getWeek,
	getCalendar,
	pinDateFn,
	transdate
}
